package co.edu.usbcali.sgi.dataaccess.dao;

import co.edu.usbcali.sgi.dataaccess.sessionFactory.HibernateSessionFactory;
import co.edu.usbcali.sgi.modelo.Ciudad;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;

import org.hibernate.criterion.Example;

import java.math.BigDecimal;

import java.util.Date;
import java.util.List;
import java.util.Set;


/**
 * A data access object (DAO) providing persistence and search support for
 * Ciudad entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 *
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class CiudadDAO implements ICiudadDAO {
    private static final Log log = LogFactory.getLog(CiudadDAO.class);

    // property constants
    //public static final String  CIUCOD = "ciuCod";
    public static final String CIUCOD = "ciuCod";

    //public static final String  CIUNOMBRE = "ciuNombre";
    public static final String CIUNOMBRE = "ciuNombre";

    private Session getSession() {
        return HibernateSessionFactory.getSession();
    }

    /**
    *
    * @param Instance
    *            Ciudad Instance to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Ciudad instance) {
        log.debug("saving Ciudad instance");

        try {
            getSession().save(instance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }

    /**
    * @param Instance
    *            Ciudad Instance to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Ciudad instance) {
        log.debug("deleting Ciudad instance");

        try {
            getSession().delete(instance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }

    /**
    *
    * @param Instance
    *            Ciudad Instance to update
    * @throws RuntimeException
    *             when the operation fails
    */
    public void update(Ciudad instance) {
        log.debug("updating Ciudad instance");

        try {
            getSession().update(instance);
            log.debug("update successful");
        } catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
        }
    }

    public Ciudad findById(String id) {
        log.debug("finding Ciudad instance with id: " + id);

        try {
            Ciudad instance = (Ciudad) getSession().get(Ciudad.class, id);

            return instance;
        } catch (RuntimeException re) {
            log.error("finding Ciudad failed", re);
            throw re;
        }
    }

    public List<Ciudad> findByExample(Ciudad instance) {
        log.debug("finding Ciudad instance by example");

        try {
            List results = getSession().createCriteria("Ciudad")
                               .add(Example.create(instance)).list();
            log.debug("find by example successful, result size: " +
                results.size());

            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }

    /**
    * Find all  Ciudad entities with a specific property value.
    *
    * @param value
    *            the property value to match
    * @param propertyName
    *            the property to search in the instance
    * @return List< Ciudad> found by query
        */
    public List<Ciudad> findByProperty(String propertyName, Object value) {
        log.debug("finding Ciudad instance with property: " + propertyName +
            ", value: " + value);

        try {
            String queryString = "from Ciudad as model where model." +
                propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find by property name failed", re);
            throw re;
        }
    }

    public List<Ciudad> findByCiuCod(Object ciuCod) {
        return findByProperty(CIUCOD, ciuCod);
    }

    public List<Ciudad> findByCiuNombre(Object ciuNombre) {
        return findByProperty(CIUNOMBRE, ciuNombre);
    }

    /**
    * Find all Ciudad entities.
    *
    * @return List<Ciudad> all Ciudad instances
    */
    public List<Ciudad> findAll() {
        log.debug("finding all Ciudad instances");

        try {
            String queryString = "from Ciudad";
            Query queryObject = getSession().createQuery(queryString);

            return queryObject.list();
        } catch (RuntimeException re) {
            log.error("find all failed", re);
            throw re;
        }
    }

    public List<Ciudad> findByCriteria(String whereCondition) {
        log.debug("finding Ciudad " + whereCondition);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Ciudad model " +
                where;
            Query query = getSession().createQuery(queryString);
            List<Ciudad> entitiesList = query.list();

            return entitiesList;
        } catch (RuntimeException re) {
            log.error("find By Criteria in Ciudad failed", re);
            throw re;
        }
    }

    public List<Ciudad> findPageCiudad(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        log.debug("finding Ciudad findPageCiudad");

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Ciudad model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Ciudad model";

                return getSession().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .list();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    public Long findTotalNumberCiudad() {
        log.debug("finding Ciudad count");

        try {
            String queryString = "select count(*) from Ciudad model";
            Query queryObject = getSession().createQuery(queryString);

            return (Long) queryObject.list().get(0);
        } catch (RuntimeException re) {
            throw re;
        }
    }
    
    public List<Ciudad> consultarCiudadPorPais(String nombre) throws Exception
	{
		String hql="FROM Ciudad c where c.pais.paiCod = :nombre";
		Query query= HibernateSessionFactory.getSession().createQuery(hql);
		query.setString("nombre", nombre);
		
		List<Ciudad> results = query.list();
		
		return results;
	}
    
    public List<Ciudad> consultarCiudad(String nombre) throws Exception{
    	
    	String hql = "FROM Ciudad c where c.ciuCod = :nombre";
    	Query query= HibernateSessionFactory.getSession().createQuery(hql);
		query.setString("nombre",nombre);
		
		List<Ciudad> result =query.list();
		
		return result;
    }
}
